/**
 * 
 */
package com.appspot.myjsf2.mail;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author worker
 * 
 */
public class MailHandlerServlet extends HttpServlet {

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		Properties props = new Properties();
		Session session = Session.getDefaultInstance(props, null);
		resp.setContentType("text/plain");
		System.out.println("getting data!");
		PrintWriter writer = resp.getWriter();

		try {
			MimeMessage message = new MimeMessage(session, req.getInputStream());
			Address[] froms = message.getFrom();
			System.out.println("From: " + froms[0].toString());
			String contentType = message.getContentType();
			Multipart content = (Multipart)message.getContent();
			int partCount = content.getCount();
			for(int i = 0; i < partCount; i++){
				
				System.out.println("body " + i + " : " +content.getBodyPart(i).getContent().toString());
			}
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
